<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
				 	<template>
						<el-select v-model="startVersion" clearable placeholder="请选择">
							<el-option
							v-for="version in startVersionOptions"
							:key="version.value"
							:label="version.label"
							:value="version.value">
							</el-option>
						</el-select>
						<el-select
							v-model="endVersion"
							clearable
							collapse-tags
							style="margin-left: 20px;"
							placeholder="请选择">
							<el-option
							v-for="item in endVersionOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value">
							</el-option>
						</el-select>
						<div style="width:200px;display:inline-block;padding-right:10px;padding-left:10px;">
							<el-input v-model="inputName" width="200" placeholder="请输入名称"></el-input>
						</div>
					</template>
					<el-button type="primary" @click="handleAdd">新增</el-button>
					<el-button type="primary" @click="getBlockArray">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="tagsList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column prop="start_app_version" label="开始版本"width="95" sortable/>
            <el-table-column prop="end_app_version" label="结束版本" width="95"sortable/>
            <el-table-column prop="iconUrl" label="图标" width="70" >
                 <template scope="scope">
                   	<a target="_blank" :href="scope.row.iconUrl" class="" style="text-align: center;display: inline-block;">
                        <img :src="scope.row.iconUrl" alt="" style="text-align: center; width:100%; padding-left:-20px;">
                    </a>
                 </template>
            </el-table-column>
			<el-table-column prop="leftTitle" label="名称" width="100" />
            <el-table-column prop="sort" label="排序" sortable/>
      
			<el-table-column prop="targetUrl" label="跳转链接" width="200" />
		
			<el-table-column prop="isValid" label="状态" sortable>
    			<template scope="scope">
                       {{scope.row.isValid=='1'?"已上线":"未上线"}}
                </template>
			</el-table-column>
			<el-table-column label="操作" width="250"  fixed="right">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleStatus(scope.$index, scope.row)"> {{scope.row.isValid=='1'?"下线":"上线"}}</el-button>
				</template>
			</el-table-column>
			  <el-table-column prop="showBadge" label="显示角标"width="100" sortable>
				<template scope="scope">
                       {{scope.row.showBadge=='true'?"显示":"不显示"}}
                </template>
			</el-table-column>
            <el-table-column prop="iosCheckShowFlag" label="IOS审核显示" width="120"sortable/>
			<el-table-column prop="umentEventId" label="友盟ID" width="200"sortable/>
			<el-table-column prop="insertTime" label="创建时间" width="170" sortable/>
			<el-table-column prop="updateTime" label="更新时间" width="170" sortable/>
			<el-table-column prop="badgeTextLogin" label="登录状态角标"width="120" sortable/>
			<el-table-column prop="badgeTextLogout" label="游客状态角标"width="120" sortable/>
			<el-table-column prop="rightTipLogin" label="登录状态提示" width="120" sortable/>
			<el-table-column prop="rightTipLogout" label="游客状态提示" width="120" sortable/>
			<el-table-column prop="type" label="响应操作"width="100"  sortable/>
			<el-table-column prop="remarks" label="描述"  sortable/>
		</el-table>
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" :rules="addFormRules" ref="editForm" >

				<el-form-item style="margin-left:0px">
					<el-row type="flex" align="middle">
						<span style="padding-left:40px">名称</span>
						<el-col :span="2" style="width: 215px;margin-left:5px;">
								<div style="width:215px;">
									<template>
										<el-input v-model="editForm.leftTitle"></el-input>
									</template>
								</div>
							</el-col>
						<span style="padding-left:10px;margin-left:25px;">排序</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="editForm.sort"></el-input>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>

				<el-form-item style="margin-left:0px">
					<el-row type="flex" align="middle">
						<span style="padding-left:20px">登录提示</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="editForm.rightTipLogin"></el-input>
									</template>
								</div>
							</el-col>
						<span style="padding-left:10px;margin-left:25px;">游客提示</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="editForm.rightTipLogout"></el-input>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>

				<el-form-item style="margin-left:0px;">
					<el-row type="flex" align="middle"  >
					<span style="padding-left:20px">登录角标</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="editForm.badgeTextLogin"></el-input>
									</template>
								</div>
							</el-col>
						<span style="padding-left:10px;margin-left:25px;">游客角标</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="editForm.badgeTextLogout"></el-input>
									</template>
								</div>
						</el-col>
					</el-row>
				</el-form-item>

				<el-form-item>
					<el-row type="flex" align="middle">
						<span style="padding-left:20px">跳转链接</span>
							<el-col :span="2" style="width: 440px;margin-left:5px;">
								<div style="width:440px;">
									<template>
										<el-input v-model="editForm.targetUrl"></el-input>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>

				<el-form-item>
					<el-row type="flex" align="middle">
						<span style="padding-left:20px">图片链接</span>
							<el-col :span="2" style="width: 440px;margin-left:5px;">
								<div style="width:440px;">
									<template>
										<el-input v-model="editForm.iconUrl"></el-input>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>

				<el-form-item style="margin-left:0px;">
					<el-row type="flex" align="middle"  >
					<span style="padding-left:20px">开始版本</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="editForm.start_app_version"></el-input>
									</template>
								</div>
							</el-col>
						<span style="padding-left:10px;margin-left:25px;">结束版本</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="editForm.end_app_version"></el-input>
									</template>
								</div>
						</el-col>
					</el-row>
				</el-form-item>

				<el-form-item style="margin-left:0px;">
					<el-row type="flex" align="middle"  >
					<span style="padding-left:20px">状态</span>
							<el-col :span="2" style="width: 180px;margin-left:25px;">
								<div style="width:160px;">
									<template>
									  	<el-radio v-model="isShow" label="1">上线</el-radio>
  										<el-radio v-model="isShow" label="0">下线</el-radio>
									</template>
								</div>
							</el-col>
							<span style="padding-left:20px">IOS审核显示</span>
							<el-col :span="2" style="width: 120px;margin-left:25px;">
								<div style="width:120px;">
									<template>
										<el-radio v-model="iosCheckShowFlag" label="1">是</el-radio>
										<el-radio v-model="iosCheckShowFlag" label="0">否</el-radio>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>
				
				<el-form-item style="margin-left:0px;">
					<el-row type="flex" align="middle"  >
					<span style="padding-left:20px">角标</span>
						<el-col :span="2" style="width: 180px;margin-left:25px;">
								<div style="width:160px;">
									<template>
									  	<el-radio v-model="isJiaoBiao" label="1">显示</el-radio>
  										<el-radio v-model="isJiaoBiao" label="0">不显示</el-radio>
									</template>
								</div>
							</el-col>
						<span>友盟ID</span>
						<el-col :span="2" style="width: 220px;margin-left:15px;">
								<div style="width:220px;">
									<template>
										<el-input v-model="editForm.umentEventId"></el-input>
									</template>
								</div>
						</el-col>
					</el-row>
				</el-form-item>


				<el-form-item label="备注">
					<el-input type="textarea" v-model="editForm.remarks"></el-input>
				</el-form-item>
				
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="addLoading">确定</el-button>
			</div>
		</el-dialog>


		<el-dialog title="添加" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" :rules="addFormRules" ref="addForm" >
				<el-form-item style="margin-left:0px">
					<el-row type="flex" align="middle">
						<span style="padding-left:40px">名称</span>
						<el-col :span="2" style="width: 215px;margin-left:5px;">
								<div style="width:215px;">
									<template>
										<el-input v-model="addForm.leftTitle"></el-input>
									</template>
								</div>
							</el-col>
						<span style="padding-left:10px;margin-left:25px;">排序</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="addForm.sort"></el-input>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>

				<el-form-item style="margin-left:0px">
					<el-row type="flex" align="middle">
						<span style="padding-left:20px">登录提示</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="addForm.rightTipLogin"></el-input>
									</template>
								</div>
							</el-col>
						<span style="padding-left:10px;margin-left:25px;">游客提示</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="addForm.rightTipLogout"></el-input>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>

				<el-form-item style="margin-left:0px;">
					<el-row type="flex" align="middle"  >
					<span style="padding-left:20px">登录角标</span>
							<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="addForm.badgeTextLogin"></el-input>
									</template>
								</div>
							</el-col>
							<span style="padding-left:10px;margin-left:25px;">游客角标</span>
							<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="addForm.badgeTextLogout"></el-input>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>

				<el-form-item>
					<el-row type="flex" align="middle">
						<span style="padding-left:20px">跳转链接</span>
							<el-col :span="2" style="width: 440px;margin-left:5px;">
								<div style="width:440px;">
									<template>
										<el-input v-model="addForm.targetUrl"></el-input>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>

				<el-form-item>
					<el-row type="flex" align="middle">
						<span style="padding-left:20px">图片链接</span>
							<el-col :span="2" style="width: 440px;margin-left:5px;">
								<div style="width:440px;">
									<template>
										<el-input v-model="addForm.iconUrl"></el-input>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>

				<el-form-item style="margin-left:0px;">
					<el-row type="flex" align="middle"  >
					<span style="padding-left:20px">开始版本</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="addForm.start_app_version"></el-input>
									</template>
								</div>
							</el-col>
						<span style="padding-left:10px;margin-left:25px;">结束版本</span>
						<el-col :span="2" style="width: 180px;margin-left:5px;">
								<div style="width:160px;">
									<template>
										<el-input v-model="addForm.end_app_version"></el-input>
									</template>
								</div>
						</el-col>
					</el-row>
				</el-form-item>

				<el-form-item style="margin-left:0px;">
					<el-row type="flex" align="middle"  >
					<span style="padding-left:20px">状态</span>
						<el-col :span="2" style="width: 180px;margin-left:25px;">
								<div style="width:160px;">
									<template>
									  	<el-radio v-model="isShow" label="1">上线</el-radio>
  										<el-radio v-model="isShow" label="0">下线</el-radio>
									</template>
								</div>
							</el-col>
						<span style="padding-left:20px">IOS审核显示</span>
						<el-col :span="2" style="width: 120px;margin-left:25px;">
								<div style="width:120px;">
									<template>
										<el-radio v-model="iosCheckShowFlag" label="1">是</el-radio>
										<el-radio v-model="iosCheckShowFlag" label="0">否</el-radio>
									</template>
								</div>
							</el-col>
					</el-row>
				</el-form-item>
				<el-form-item style="margin-left:0px;">
					<el-row type="flex" align="middle"  >
					<span style="padding-left:20px">角标</span>
						<el-col :span="2" style="width: 180px;margin-left:25px;">
								<div style="width:160px;">
									<template>
									  	<el-radio v-model="isJiaoBiao" label="1">显示</el-radio>
  										<el-radio v-model="isJiaoBiao" label="0">不显示</el-radio>
									</template>
								</div>
							</el-col>
						<span>友盟ID</span>
						<el-col :span="2" style="width: 220px;margin-left:15px;">
								<div style="width:220px;">
									<template>
										<el-input v-model="addForm.umentEventId"></el-input>
									</template>
								</div>
						</el-col>
					</el-row>
				</el-form-item>

				<el-form-item label="备注">
					<el-input type="textarea" v-model="addForm.remarks"></el-input>
				</el-form-item>
				
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">确定</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	import {
		addBlock,
		updateBlock,
		getBlockList,
		appBlockGetVersion

	} from '../../api/api';
	export default {
		data() {
			return {
				filters: {
					keyword: ''
				},
				isJiaoBiao:'0',
				isHint:'0',
				isShow:'1',
				iosCheckShowFlag:'1',
				checkList: [],
				calculateTime:'',
				isValid: '1',
				value1: '',
				tagsList: [],
				total: 0,
				page: 1,
				pageSize: 10,
				listLoading: false,
				sels: [], //列表选中列
				editFormVisible: false, //编辑界面是否显示
				editLoading: false,
				editFormRules: {
					appName: [{
						required: true,
						message: '请输入应用名称',
						trigger: 'blur'
					}],
				},
				startVersion:'',
				endVersion:'',
				startVersionOptions:[],
				endVersionOptions:[],
				inputName:'',
			    searchParams:'',
				//编辑界面数据
				editForm: {
					title: '',
					describe: '',
					remarks: '',
					url:"",
					vipNow:0,
					haveCar:0,
					jiazhao:0,
					fufei:0,
					haveRecord:0,
					activityStartTime: '',
					activityEndTime: '',
					registerStartTime: '',
					registerEndTime: '',
				},
				pushForm: {
					    position:"",
                        block:"",
                        sort:"",
                        iconUrl:"",
                        leftTitle:"",
                        rightTipLogin:"",
                        rightTipLogout:"",
                        umentEventId:"",
                        targetUrl:"",
                        type:"",
                        logoutHindFlag:"",
                        isValid:"",
                        iosCheckShowFlag:"",
                        remarks:"",
                        insertTime:"",
                        updateTime:"",
                        start_app_version:"",
                        end_app_version:"",
                        badgeTextLogin:"",
                        badgeTextLogout:"",
                        showBadge:"",
				},
				addFormVisible: false, //新增界面是否显示
				addLoading: false,
				pushFormVisible:false,
				addFormRules: {
				},
				//新增界面数据
				addForm: {
				
				}


			}
		},
		methods: {
			handleCurrentChange(val) {
				this.page = val;
				this.getBlockArray();
			
			},
			getAppVersion(){
				console.log('getAppVersion')
				var that = this;
				appBlockGetVersion().then((res)=>{
					let result = res.data
					if (result.code == "1000") {
						that.startVersionOptions = []
						result.data.startList.map(item => {
							that.startVersionOptions.push({
								value:item,
								label:item
							})
						});
						that.endVersionOptions = []
						result.data.endList.map(item => {
							console.log('item',item)
							that.endVersionOptions.push({
								value:item+'',
								label:item+''
							})
						});
						console.log('that',that.endVersionOptions)
					}
				})
			},
			//获取用户列表
			getBlockArray() {
                this.listLoading = true;
				let param = Object.assign({}, {
                    page:this.page,
                    limit:this.pageSize,
					startVersion:this.startVersion,
					endVersion:this.endVersion,
					inputName:this.inputName
                });
				getBlockList(param).then((res) => {
					let result = res.data
					if (result.code == "1000") {
						this.tagsList = res.data.data.cxBlocks.map(item => {
							return Object.assign({}, item, {
								insertTime: util.formatDate.format(new Date(item.insertTime), 'yyyy-MM-dd hh:mm:ss'),
								updateTime: util.formatDate.format(new Date(item.updateTime), 'yyyy-MM-dd hh:mm:ss'),
							})
						});
						this.total = res.data.data.total
					}
					this.listLoading = false;
				});
			},
		
			handleStatus: function(index, row) {
				var status = 0;
				if(row.isValid==1){
					row.isValid  = 0
				}else{
					row.isValid  = 1
				}
				updateBlock(row).then((res) => {
					this.editLoading = false;
					this.$message({
						message: '提交成功',
						type: 'success'
					});
					
					this.editFormVisible = false;
					this.getBlockArray();
				});
			},
			//显示编辑界面
			handleEdit: function(index, row) {
				this.editFormVisible = true;
				console.log("--",row);
				this.editForm = Object.assign({}, row);
				this.isShow = this.editForm.isValid +''
				this.iosCheckShowFlag = this.editForm.iosCheckShowFlag +''
				if(this.editForm.showBadge == 'true'){
					this.isJiaoBiao=='1'
				}else{
					this.isJiaoBiao=='0'
				}
			},
			//显示新增界面
			handleAdd: function() {
				this.addForm = {}
				this.iosCheckShowFlag = '0'
				this.isJiaoBiao = '0'
				this.isShow = '0'
				this.addFormVisible = true;
			},
			//编辑
			editSubmit: function() {
				this.$confirm('确认提交吗？', '提示', {}).then(() => {
					this.editLoading = true;
					var str = "";
					this.checkList.forEach(function(element) {
						str += element+",";
					}, this);
					str = str.substring(0,str.length-1);
					this.editForm.checkList = str
					console.log("-----------------",this.editForm)
					this.editForm.isValid = Number(this.isShow)
					if(this.isJiaoBiao=='1'){
						this.editForm.showBadge = 'true'
					}else{
						this.editForm.showBadge = 'false'
					}
					
					this.editForm.iosCheckShowFlag = Number(this.iosCheckShowFlag)
					updateBlock(this.editForm).then((res) => {
						this.editLoading = false;
						this.$message({
							message: '提交成功',
							type: 'success'
						});
						this.editFormVisible = false;
						this.getBlockArray();
					});
				});
			},
			//新增
			addSubmit: function() {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							var str = "";
							this.addForm.showBadge = 'false'
							if(this.isJiaoBiao=='1'){
								this.addForm.showBadge = 'true'
							}

							this.addForm.isValid =this.isShow
							this.addForm.iosCheckShowFlag = this.iosCheckShowFlag 
							this.checkList.forEach(function(element) {
								str += element+",";
							}, this);
							str = str.substring(0,str.length-1);
							this.addForm.checkList = str;
							let para = Object.assign({}, this.addForm);
							console.log("列表",para)
							addBlock(para).then((res) => {
								this.addLoading = false;
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getBlockArray();
							});
						});
					}
				});
			},
			selsChange: function(sels) {
				this.sels = sels;
			},
		},
		
		mounted() {
			this.getBlockArray();
			this.getAppVersion();
		}
	}
</script>

<style scoped>

</style>


